package com.azdebugit.java8.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: itdebug
 * @Email: itdebug
 * @version:: design_pattern_and_otherutils
 * @description:
 * @create: 2021/11/25 1:07.
 */
public class TestFutureMain {
    public UserInfo getUserInfo(Long id) throws InterruptedException, ExecutionException {
        ExecutorService executor = Executors.newCachedThreadPool();
        final UserInfo userInfo = new UserInfo();
        CompletableFuture userFuture = CompletableFuture.supplyAsync(() -> {
            getRemoteUserAndFill(id, userInfo);
            return Boolean.TRUE;
        }, executor);

        CompletableFuture bonusFuture = CompletableFuture.supplyAsync(() -> {
            getRemoteBonusAndFill(id, userInfo);
            return Boolean.TRUE;
        }, executor);

        CompletableFuture growthFuture = CompletableFuture.supplyAsync(() -> {
            getRemoteGrowthAndFill(id, userInfo);
            return Boolean.TRUE;
        }, executor);
        CompletableFuture.allOf(userFuture, bonusFuture, growthFuture).join();

        userFuture.get();
        bonusFuture.get();
        growthFuture.get();

        return userInfo;
    }

    private void getRemoteGrowthAndFill(Long id, UserInfo userInfo) {

    }

    private void getRemoteBonusAndFill(Long id, UserInfo userInfo) {

    }

    private void getRemoteUserAndFill(Long id, UserInfo userInfo) {

    }
}
